Page({
  data: {
    tasks: [],
    showCodeModal: false,
    showGestureModal: false,
    currentTaskId: null,
    code: '',
    selectedPoints: [],
    gestureSequence: ''
  },

  onLoad() {
    this.loadTasks();
  },

  // 加载签到任务
  loadTasks() {
    const token = wx.getStorageSync('token');
    wx.request({
      url: 'http://localhost:8080/api/student/attendance/tasks',
      method: 'GET',
      header: {
        'Authorization': token
      },
      success: (res) => {
        if (res.data.code === 1) {
          this.setData({
            tasks: res.data.data
          });
        }
      }
    });
  },

  // 一键签到
  quickSign(e) {
    const taskId = e.currentTarget.dataset.id;
    const token = wx.getStorageSync('token');
    
    wx.request({
      url: 'http://localhost:8080/api/student/attendance/quick',
      method: 'POST',
      header: {
        'Authorization': token
      },
      data: {
        taskId: taskId
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '签到成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: res.data.msg || '签到失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 显示签到码输入框
  showCodeInput(e) {
    this.setData({
      showCodeModal: true,
      currentTaskId: e.currentTarget.dataset.id,
      code: ''
    });
  },

  // 隐藏签到码输入框
  hideCodeInput() {
    this.setData({
      showCodeModal: false
    });
  },

  // 输入签到码
  onCodeInput(e) {
    this.setData({
      code: e.detail.value
    });
  },

  // 提交签到码
  submitCode() {
    const { currentTaskId, code } = this.data;
    const token = wx.getStorageSync('token');
    
    wx.request({
      url: 'http://localhost:8080/api/student/attendance/code',
      method: 'POST',
      header: {
        'Authorization': token
      },
      data: {
        taskId: currentTaskId,
        code: code
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '签到成功',
            icon: 'success'
          });
          this.hideCodeInput();
        } else {
          wx.showToast({
            title: res.data.msg || '签到失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 位置签到
  locationSign(e) {
    const taskId = e.currentTarget.dataset.id;
    const token = wx.getStorageSync('token');
    
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        wx.request({
          url: 'http://localhost:8080/api/student/attendance/location',
          method: 'POST',
          header: {
            'Authorization': token
          },
          data: {
            taskId: taskId,
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: (res) => {
            if (res.data.code === 1) {
              wx.showToast({
                title: '签到成功',
                icon: 'success'
              });
            } else {
              wx.showToast({
                title: res.data.msg || '签到失败',
                icon: 'none'
              });
            }
          }
        });
      },
      fail: () => {
        wx.showToast({
          title: '获取位置失败',
          icon: 'none'
        });
      }
    });
  },

  // 显示手势绘制区域
  showGesturePad(e) {
    this.setData({
      showGestureModal: true,
      currentTaskId: e.currentTarget.dataset.id,
      selectedPoints: [],
      gestureSequence: ''
    });
  },

  // 隐藏手势绘制区域
  hideGesturePad() {
    this.setData({
      showGestureModal: false
    });
  },

  // 点击手势点
  onGesturePointTap(e) {
    const index = e.currentTarget.dataset.index;
    let { selectedPoints, gestureSequence } = this.data;
    
    if (!selectedPoints.includes(index)) {
      selectedPoints.push(index);
      gestureSequence += (index + 1).toString();
      
      this.setData({
        selectedPoints,
        gestureSequence
      });
    }
  },

  // 提交手势
  submitGesture() {
    const { currentTaskId, gestureSequence } = this.data;
    const token = wx.getStorageSync('token');
    
    wx.request({
      url: 'http://localhost:8080/api/student/attendance/gesture',
      method: 'POST',
      header: {
        'Authorization': token
      },
      data: {
        taskId: currentTaskId,
        gestureSequence: gestureSequence
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '签到成功',
            icon: 'success'
          });
          this.hideGesturePad();
        } else {
          wx.showToast({
            title: res.data.msg || '签到失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 扫描二维码
  scanQRCode(e) {
    const taskId = e.currentTarget.dataset.id;
    const token = wx.getStorageSync('token');
    
    wx.scanCode({
      success: (res) => {
        wx.request({
          url: 'http://localhost:8080/api/student/attendance/qrcode',
          method: 'POST',
          header: {
            'Authorization': token
          },
          data: {
            taskId: taskId,
            qrCode: res.result
          },
          success: (res) => {
            if (res.data.code === 1) {
              wx.showToast({
                title: '签到成功',
                icon: 'success'
              });
            } else {
              wx.showToast({
                title: res.data.msg || '签到失败',
                icon: 'none'
              });
            }
          }
        });
      },
      fail: () => {
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        });
      }
    });
  }
}); 